package com.sgzn.sgpay.service.tcp.impl;

import com.sgzn.sgpay.model.response.SgsResultResponse;
import com.sgzn.sgpay.service.support.SgsResponseUtil;
import com.sgzn.sgpay.service.tcp.SgTcpServer;
import lombok.extern.slf4j.Slf4j;

import java.net.Socket;

/**
 * Tcp 调用公共类
 * @author huangsenguang
 * @version Id: TcpBaseTemplate.java, v 0.1 2018/7/19 18:22 huangsenguang Exp $$
 */
@Slf4j
public class TcpBaseTemplate {
    /**
     * 执行
     * @param deviceId 设备id
     * @param context 请求报文
     * @return
     */
    public SgsResultResponse<String> execute (String deviceId, String context) {
        Socket socket =  SgTcpServer.socketList.get(deviceId);
        if (socket == null){
            log.info("设备 {} 连接异常，请检查网络",deviceId);
            return null;
        }
        log.info("请求报文 {}" , context );
        try {
            socket.getOutputStream().write(context.getBytes("UTF-8"));
        }catch (Exception e){
            e.printStackTrace();
        }
        return SgsResponseUtil.INSTANCE.success("请求报文:"+context+"</br>发送求成功");
    }
}

